<?php
/**
 * @since      File available since Release v1.1
 */
defined('Inbearshop') or exit('Access Invalid!');
define('MYSQL_RESULT_TYPE',1);
class paymentControl extends BaseHomeControl{
	public function __construct(){
		parent::__construct();
        Language::read('indexs');
        Tpl::output('index_sign','index');
	}
	
	public function balancepaymentcashOp(){
		
		$model = Model();
		
		if(!empty($_POST)){	
			
			$auction = $model->table('auction')->where(array('id'=>$_POST['auction_id']))->find();
			$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
			if($user['paypasswd']!=md5($_POST['paypasswd'])){
				echo json_encode(array('state'=>'0','error'=>'支付密码不正确'));
				exit;
			}
			
			
			$price = $user['price']-$auction['cashprice'];
			$res = $model->table('user')->where(array('user_id'=>$user['user_id']))->update(array('price'=>$price));

			if($res){
			
				$params = array();
				$params['user_id'] = $user['user_id'];
				$params['mingxi_type'] = '1';
				$params['mingxi_price'] = $auction['cashprice'];
				$params['add_time'] = time();
				$params['mingxi_reason'] = '保证金支付';
				$params['mingxi_pos'] = '0';
				$model->table('mingxi')->insert($params);
			
				$update = array();
				$update['user_id'] = $user['user_id'];
				$update['pre_order_sn'] = 'pre'.$this->snOrder();
				$update['auction_id'] = $auction['id'];
				$update['is_cash'] = 1;
				$update['cprice'] = $auction['cashprice'];
				$update['paymentname'] = 'balance';
				$update['paymenttime'] = time();
			
			
				$res1 = $model->table('cash_log')->insert($update);
				if($res1){
					if($_COOKIE['user_id']==$auction['seller_id']){
						$model->table('auction')->where(array('id'=>$auction['id']))->update(array('is_fabu'=>1,'add_time'=>time(),'is_state'=>'0'));
						$this->tuiinfo($auction['id'],$auction['end_time']);
						echo json_encode(array('state'=>'1'));
						exit;
					}
					$params = array();
					$params['auction_id'] = $auction['id'];
					$params['auction_price'] = $_POST['auction_price'];
					$params['add_time'] = time();
					$params['user_id'] = $user['user_id'];
					$params['nickname'] = $user['nickname'];
					$params['avatar'] = $user['avatar'];
					$res1 = $model->table('auction_log')->insert($params,true);
					if($res1){
						$update1 = array();
						$update1['leadingprice'] = $params['auction_price'];
						$update1['auction_log_id'] = $res1;
						if(($auction['yikoujia']>0)&&($_POST['auction_price']>=$auction['yikoujia'])){
							$update1['yikoujia_end'] = 1;
						}
						
						if(($auction['end_time']-300)<time()){
							$update1['end_time'] = $auction['end_time']+300;
						}
						
						$model->table('auction')->where(array('id'=>$auction['id']))->update($update1);
						if($update1['yikoujia_end'] ==1){
							echo json_encode(array('state'=>1));
							exit;
						}
						


						$auction = $model->table('auction')->where(array('id'=>$auction['id']))->find();
						$seller = $model->table('user')->where(array('user_id'=>$auction['seller_id']))->find();
						
						$data = array();
						$data['touser'] = $seller['openid'];
						$data['template_id'] = $this->getTemplateId(11);
						$data['url'] = BASE_SITE_URL.'/index.php?act=index&auction_id='.$auction['id'];
						$data['data']['first']['value'] = "您的拍品【{$params['nickname']}】出价{$_POST['auction_price']}元，目前领先";
						$data['data']['number']['value'] = "{$auction['auction_sn']}";
						$data['data']['name']['value'] = "{$auction['auction_description']}";
						$data['data']['remark']['value'] = '提醒：'.date("m月d日 H:i",$auction['end_time']).'拍卖结束';
						$data1 = json_encode($data);
						$res = $this->send_template_message($data1);
						
						$seller_log = array();
						$seller_log['auction_id'] = $auction['id'];
						$seller_log['uid'] = $auction['seller_id'];
						$seller_log['message'] = $data['data']['first']['value'];
						$seller_log['add_time'] = time();
						$model->table('seller_log')->insert($seller_log);
						
						$condition = array();
						$condition['auction_log.auction_id'] = $auction['id'];
						$field = 'auction_log.id as id,user.openid as openid';
						$on = 'auction_log.user_id = user.user_id';
						$userlist = $model->field($field)->table('auction_log,user')->join('left')->on($on)->where($condition)->select();
						
						if(!empty($userlist)){
							$arr = array();
							foreach($userlist as $vvv){
						
								if($user['openid']==$vvv['openid']){
									continue;
								}
								
								if(in_array($vvv['openid'],$arr)){
									continue;
								}
						
								$data = array();
								$data['touser'] = $vvv['openid'];
								$data['template_id'] = $this->getTemplateId(11);
								$data['url'] = BASE_SITE_URL.'/index.php?act=index&auction_id='.$auction['id'];
								$data['data']['first']['value'] = "【{$params['nickname']}】出价{$_POST['auction_price']}元，目前领先";
								$data['data']['number']['value'] = "{$auction['auction_sn']}";
								$data['data']['name']['value'] = "{$auction['auction_description']}";
								$data['data']['remark']['value'] = '提醒：'.date("m月d日 H:i",$auction['end_time']).'拍卖结束';;
								$data1 = json_encode($data);
								$this->send_template_message($data1);
								$arr[] = $vvv['openid'];
							}
						}
						
						echo json_encode(array('state'=>1));
						exit;
					}
				}
			}
		}
		
		$auction = $model->table('auction')->where(array('id'=>intval($_GET['auction_id'])))->find();
		Tpl::output('auction',$auction);
		Tpl::showpage('payment.balancecash');
	}
	
	public function paycashsuccessOp(){
		$model = Model();
		$auction = $model->table('auction')->where(array('id'=>intval($_GET['auction_id'])))->find();
		Tpl::output('auction',$auction);
	
		Tpl::showpage('payment.paycashsuccess');
	}
	
	public function paymentcashOp(){
		
		if(!in_array($_POST['paymentname'],array('weixin','balance'))){
			echo json_encode(array('state'=>'0'));
			exit;
		}
		$model = Model();
		$auction = $model->table('auction')->where(array('id'=>$_POST['auction_id']))->find();
		$user = $model->table('user')->where(array('user_id'=>$_POST['user_id']))->find();
		$paymentname = $_POST['paymentname'];

		$user = $model->table('user')->where(array('user_id'=>$user['user_id']))->find();
	
		if(intval($user['price'])<intval($auction['cashprice'])){
			echo json_encode(array('state'=>'0','余额不足'));
			exit;
		}
		
		echo json_encode(array('state'=>'1'));
		exit;
	
	}
	

	
	public function cashOp(){
		
		$auction_id = intval($_GET['auction_id']);
		$model = Model();
		$auction = $model->table('auction')->where(array('id'=>$auction_id))->find();
		Tpl::output('auction',$auction);
		
		$update = array();
		$update['user_id'] = $_COOKIE['user_id'];
		$update['pre_order_sn'] = 'pre'.$this->snOrder();
		$update['auction_id'] = $auction['id'];
		$update['is_cash'] = '0';
		$update['cprice'] = $auction['cashprice'];
		$update['paymentname'] = 'weixin';
		$update['paymenttime'] = time();
		
		$res = $model->table('cash_log')->insert($update,true);
		$cash_log = $model->table('cash_log')->where(array('id'=>$res))->find();
		Tpl::output('cash_log',$cash_log);
		
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		Tpl::output('user',$user);
		
		Tpl::showpage('payment.cash.weixin');
	}
	
	public function weixinOp(){
		$condition = array();
		$condition['id'] = intval($_GET['order_id']);
		$condition['user_id'] = $_COOKIE['user_id'];
		
		$model = Model();
		$order = $model->table('order')->where($condition)->find();
		if(empty($order)){
			exit;
		}
		
		$model->table('order')->where($condition)->update(array('order_address'=>intval($_GET['address'])));
		
		Tpl::output('order',$order);
		
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		Tpl::output('auction',$auction);
		
		$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();
		Tpl::output('user',$user);
		
		Tpl::showpage('payment.weixin');
	}
	
	public function balancepaymentOp(){
		
		$model = Model();
		
		if(!empty($_POST)){
			$order = $model->table('order')->where(array('id'=>intval($_POST['order_id'])))->find();
			$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
			if(empty($order)){
				echo json_encode(array('state'=>'0','error'=>'订单不存在'));
				exit;
			}
			
			$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();

			if($user['paypasswd']!=md5($_POST['paypasswd'])){
				echo json_encode(array('state'=>'0','error'=>'支付密码不正确'));
				exit;
			}
			
			
			$price = $user['price']-$order['price'];
			$res = $model->table('user')->where(array('user_id'=>$order['user_id']))->update(array('price'=>$price));
				
			$params = array();
			$params['user_id'] = $order['user_id'];
			$params['mingxi_type'] = '1';
			$params['mingxi_price'] = $order['price'];
			$params['add_time'] = time();
			$params['mingxi_reason'] = '拍品支付';
			$params['mingxi_pos'] = '0';
			$model->table('mingxi')->insert($params);
			if($res){
				$user_address = $model->table('address')->where(array('user_id'=>$_COOKIE['user_id'],'is_default'=>1))->find();
			
				$prov = $model->table('region')->where(array('region_code'=>$user_address['pid']))->find();
				$user_address['prov_name'] = $prov['region_name'];
				
				$city = $model->table('region')->where(array('region_code'=>$user_address['cid']))->find();
				$user_address['city_name'] = $city['region_name'];
				
				if(!empty($user_address['did'])){
					$dis = $model->table('region')->where(array('region_code'=>$user_address['did']))->find();
					$user_address['dis_name'] = $dis['region_name'];
				}else{
					$user_address['dis_name'] = '';
				}
				
				$update = array();
				$update['order_state'] = 1;
				$update['order_address'] = $user_address['id'];
				$update['payment_time'] = time();
				$update['currentexchange'] = 0;
				$model->table('order')->where(array('id'=>$order['id']))->update($update);
			
				$message = '买家已经付款! <br>尊敬的卖家，您的拍品买家已经付款';
				$this->addsellerlog($order['order_auction_id'],$message,$auction['seller_id']);			
				
				$seller = $model->table('user')->where(array('user_id'=>$auction['seller_id']))->find();
				$data = array();
				$data1 = array();
				$data['touser'] = $seller['openid'];
				$data['template_id'] = $this->getTemplateId(10);
				$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$order['order_auction_id'];
				$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,卖家【{$user['nickname']}】已经付款成功!";
				$data['data']['orderProductPrice']['value'] = "{$order['price']}元";
				$data['data']['orderProductName']['value'] = "{$auction['auction_description']}";
				$data['data']['orderAddress']['value'] = "{$user_address['prov_name']}{$user_address['city_name']}{$user_address['dis_name']}{$user_address['address']}";
				$data['data']['orderName']['value'] = "{$order['order_sn']}";
				$data1 = json_encode($data);
				$this->send_template_message($data1);
				
				if($auction['cashprice']>0){
					//买家保证金退还
					$user_cash_log = $model->table('cash_log')->where(array('user_id'=>$order['user_id'],'auction_id'=>$auction['id']))->find();
					if($user_cash_log['paymentname']=='weixin'){
						require_once BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php";
						$out_trade_no = $user_cash_log['pre_order_sn'];
						$refund_fee = $user_cash_log["cprice"]*100;
						//商户退款单号，商户自定义，此处仅作举例
						$out_refund_no = "refund_ucash_sn".time();
						//总金额需与订单号out_trade_no对应，demo中的所有订单的总金额为1分
						$total_fee = $user_cash_log["cprice"]*100;
						//使用退款接口
						$refund = new Refund_pub();
						$refund->setParameter("out_trade_no","$out_trade_no");//商户订单号
						$refund->setParameter("out_refund_no","$out_refund_no");//商户退款单号
						$refund->setParameter("total_fee","$total_fee");//总金额
						$refund->setParameter("refund_fee","$refund_fee");//退款金额
						$refund->setParameter("op_user_id",WxPayConf_pub::MCHID);//操作员
						//调用结果
						$refundResult = $refund->getResult();
					
						$data = array();
						$data1 = array();
						$data['touser'] = $user['openid'];
						$data['template_id'] = $this->getTemplateId(6);
						$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$order['id'];
						$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】,您参与的‘{$auction['auction_description']}’拍卖付款成功，保证金退还至您微信钱包";
						$data['data']['reason']['value'] = '付款成功';
						$data['data']['refund']['value'] = $auction['cashprice'].'元';
						$data['data']['remark']['value'] = '如未收到退款，请联系客服';
							
						//明细记录
						$params = array();
						$params['user_id'] = $order['user_id'];
						$params['mingxi_type'] = '0';
						$params['mingxi_price'] = $auction['cashprice'];
						$params['add_time'] = time();
						$params['mingxi_reason'] = '保证金退还';
						$params['mingxi_pos'] = '1';
						$model->table('mingxi')->insert($params);
					
					}else{
						$model->table('user')->where(array('user_id'=>$order['user_id']))->setInc('price',$auction['cashprice']);
							
						$data = array();
						$data1 = array();
						$data['touser'] = $user['openid'];
						$data['template_id'] = $this->getTemplateId(6);
						$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$order['id'];
						$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】,您参与的‘{$auction['auction_description']}’拍卖付款成功，保证金退还至您的余额";
						$data['data']['reason']['value'] = '付款成功';
						$data['data']['refund']['value'] = $auction['cashprice'].'元';
						$data['data']['remark']['value'] = '如未收到退款，请联系客服';
							
						//明细记录
						$params = array();
						$params['user_id'] = $order['user_id'];
						$params['mingxi_type'] = '0';
						$params['mingxi_price'] = $auction['cashprice'];
						$params['add_time'] = time();
						$params['mingxi_reason'] = '保证金退还';
						$params['mingxi_pos'] = '0';
						$model->table('mingxi')->insert($params);
					}
					
					$data1 = json_encode($data);
					$this->send_template_message($data1);
				}
				
				echo json_encode(array('state'=>'1'));
				exit;
			}
			exit;
		}
		
		$order = $model->table('order')->where(array('id'=>intval($_GET['order_id'])))->find();
		if($order['user_id']!=$_COOKIE['user_id']){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		Tpl::output('order',$order);
		Tpl::showpage('payment.balancepay');
	}
	
	public function paysuccessOp(){
		$model = Model();
		$auction = $model->table('auction')->where(array('id'=>intval($_GET['auction_id'])))->find();				
		Tpl::output('auction',$auction);
		
		$order = $model->table('order')->where(array('order_sn'=>$auction['auction_order_sn']))->find();
		Tpl::output('order',$order);
		Tpl::showpage('payment.paysuccess');
	}
	
	public function paymentOp(){
		$model = Model();
		$order = $model->table('order')->where(array('id'=>intval($_POST['order_id'])))->find();
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		if(empty($order)){
			echo json_encode(array('state'=>'0'));
			exit;
		}
		
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		if(empty($user['paypasswd'])){
			echo json_encode(array('state'=>'2'));//2.余额支付需要支付密码
			exit;
		}
		
		if(!in_array($_POST['paymentname'],array('weixin','balance'))){
			echo json_encode(array('state'=>'0','error'=>'请选择支付方式'));
			exit;
		}

		$paymentname = $_POST['paymentname'];

		$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();
		if(intval($user['price'])<intval($order['price'])){
			echo json_encode(array('state'=>'0','error'=>'余额不足,请充值'));
			exit;
		}else{
			echo json_encode(array('state'=>'1'));
			exit;
		}
		
		exit;

	}
	
	
	public function orderpaymentOp(){
		$model = Model();
		$order = $model->table('order')->where(array('id'=>intval($_GET['order_id'])))->find();
		Tpl::output('order',$order);
		
		if($order['user_id']!=$_COOKIE['user_id']){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		Tpl::output('auction',$auction);
		
		$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();
		Tpl::output('user',$user);
		
		$condition = array();
		$condition['user_id'] = $order['user_id'];
		$condition['is_default'] = 1;
		$address = $model->table('address')->where($condition)->find();
		if(!empty($address)){
			$prov = $model->table('region')->where(array('region_code'=>$address['pid']))->find();
			$address['prov_name'] = $prov['region_name'];
			
			$city = $model->table('region')->where(array('region_code'=>$address['cid']))->find();
			$address['city_name'] = $city['region_name'];
			
			if(!empty($address['did'])){
				$dis = $model->table('region')->where(array('region_code'=>$address['did']))->find();
				$address['dis_name'] = $dis['region_name'];
			}else{
				$address['dis_name'] = '';
			}
		}

		Tpl::output('address',$address);
		
		Tpl::showpage('payment.order');
	}
	
	public function addressaddOp(){
		
		if(isset($_POST)&&!empty($_POST)){
			$model = Model();
			$model->table('address')->where(array('user_id'=>$_COOKIE['user_id']))->update(array('is_default'=>2));
			
			$params = array();
			$params['user_id'] = $_COOKIE['user_id']; 
			$params['name'] = trim($_POST['name']);
			$params['tele'] = trim($_POST['tele']);
			$params['address'] = trim($_POST['address']);
			$params['pid'] = intval($_POST['pid']);
			$params['cid'] = intval($_POST['cid']);
			$params['did'] = intval($_POST['did']);
			
			$params['is_default'] = 1;

			$res = $model->table('address')->insert($params);
			if($res){
				echo json_encode(array('state'=>1));
			}else{
				echo json_encode(array('state'=>0));
			}
			exit;
		}
		
		$model = Model();
		$list = $model->table('region')->where(array('father_id'=>'0'))->select();

		Tpl::output('prov',$list);
		Tpl::showpage('payment.addressadd');
	}
	
	public function getcitysonOp(){
		$model = Model();
		$list = $model->table('region')->where(array('father_id'=>intval($_GET['pid'])))->select();

		$html = '';
		if(!empty($list)){
			$html.='<select name="cid">';
			$html.= '<option value="">请选择...</option>';
			foreach($list as $val){
				$html.='<option value="'.$val['region_code'].'">'.$val['region_name'].'</option>';
			}
			$html.='</select>';
		}
		
		echo $html;
		exit;
	}
	
	public function getareasonOp(){
		$model = Model();
		$list = $model->table('region')->where(array('father_id'=>intval($_GET['cid'])))->select();
		
		$html = '';
		if(!empty($list)){
			$html.='<select name="did">';
			$html.= '<option value="">请选择...</option>';
			foreach($list as $val){
				$html.='<option value="'.$val['region_code'].'">'.$val['region_name'].'</option>';
			}
			$html.='</select>';
		}
		
		echo $html;
		exit;
	}
	
	/*
	 * 收货地址
	 */
	public function addresslistOp(){
		
		$model = Model();
		$list = $model->table('address')->where(array('user_id'=>$_COOKIE['user_id']))->order('id desc')->select();

		
		if(!empty($list)){
			foreach($list as $key=>$val){
				$prov = $model->table('region')->where(array('region_code'=>$val['pid']))->find();
				$list[$key]['prov_name'] = $prov['region_name'];
				
				$city = $model->table('region')->where(array('region_code'=>$val['cid']))->find();
				$list[$key]['city_name'] = $city['region_name'];
				
				if(!empty($val['did'])){
					$dis = $model->table('region')->where(array('region_code'=>$val['did']))->find();
					$list[$key]['dis_name'] = $dis['region_name'];
				}else{
					$list[$key]['dis_name'] = '';
				}
				
			}
		}
		
		Tpl::output('address',$list);
		Tpl::showpage('payment.addresslist');
	}
	
	/*
	 * 默认收货地址
	 */
	public function defaultaddressOp(){
		$model = Model();
		$model->table('address')->where(array('user_id'=>$_COOKIE['user_id']))->update(array('is_default'=>2));
		
		$condition = array();
		$condition['id'] = intval($_POST['addressid']);
		$condition['user_id'] = $_COOKIE['user_id'];
		
		$res = $model->table('address')->where($condition)->update(array('is_default'=>1));
		if($res){
			$model->table('order')->where(array('id'=>$_POST['order_id']))->update(array('order_address'=>$condition['id']));
			
			echo json_encode(array('state'=>1));
		}else{
			echo json_encode(array('state'=>0));
		}
		exit;
	}
	
	public function deladdressOp(){
		$model = Model();
		$condition = array();
		$condition['id'] = intval($_POST['addressid']);
		$condition['user_id'] = $_COOKIE['user_id'];
		
		$res = $model->table('address')->where($condition)->delete();
		if($res){
			echo json_encode(array('state'=>1));
		}else{
			echo json_encode(array('state'=>0));
		}
		exit;
	}
	
	public function kaitongOp(){
		if(isset($_POST)&&!empty($_POST)){
			$params = array();
			$params['paypasswd'] = md5($_POST['paypasswd']);
			$params['cardname'] = trim($_POST['cardname']);
			$params['cardid'] = trim($_POST['cardid']);

			$condition = array();
			$condition['user_id'] = $_COOKIE['user_id'];
			$model = Model();
			$model->table('user')->where($condition)->update($params);
			echo json_encode(array('state'=>1));
			exit;
		}
		
		$ref_url = request_uri();
		Tpl::output('ref_url',$ref_url);
		$model = Model();
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		Tpl::output('user',$user);
		Tpl::showpage('payment.kaitong');
	}
	
	public function sendoutOp(){
		if(isset($_POST)&&!empty($_POST)){

			$params = array();
			if(intval($_POST['is_express'])==1){
				$params['is_express'] = 1;
				$params['express'] = $_POST['code'];
				$params['expresscode'] = $_POST['com'];
				$params['order_state'] = 2;
				$params['fahuo_time'] = time();
			}else{
				$params['is_express'] = 2;
				$params['order_state'] = 2;
				$params['fahuo_time'] = time();
			}
			$model = Model();			
			$condition = array();
			$condition['id'] = $_POST['order_id'];
			$order = $model->table('order')->where($condition)->find();
			if(empty($order)){
				echo json_encode(array('state'=>0));
				exit;
			}
			$model->table('order')->where($condition)->update($params);
			$message = '卖家已经发货! <br>尊敬的买家，您的拍品已经发货';
			$this->addsellerlog($order['order_auction_id'],$message,$order['user_id']);
			
			$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();
			$auction = $model->table('auction')->where(array('auction_order_sn'=>$order['order_sn']))->find();
			
			$data = array();
			$data['touser'] = $user['openid'];
			$data['template_id'] = $this->getTemplateId(1);
			$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$order['id'];
			$data['data']['first']['value'] = "尊敬的{$user['nickname']},您的拍品已经发货";
			$data['data']['keyword1']['value'] = $_POST['com'];
			$data['data']['keyword2']['value'] = $_POST['code'];
			$data['data']['keyword3']['value'] = $auction['auction_description'];
			$data['data']['keyword4']['value'] = '1';
			$data['data']['remark']['value'] = '提醒：收到拍品后，请及时收货!（发货7天后自动确认收货）';
			$data1 = json_encode($data);
			$t = $this->send_template_message($data1);

			echo json_encode(array('state'=>1));
			exit;		
		}
		
		$model = Model();
		$order = $model->table('order')->where(array('id'=>intval($_GET['order_id'])))->find();
		
		if($order['seller_id']!=$_COOKIE['user_id']){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		$address = $model->table('address')->where(array('id'=>$order['order_address']))->find();

		$prov = $model->table('region')->where(array('region_code'=>$address['pid']))->find();
		$address['prov_name'] = $prov['region_name'];
		
		$city = $model->table('region')->where(array('region_code'=>$address['cid']))->find();
		$address['city_name'] = $city['region_name'];
		
		if(!empty($address['did'])){
			$dis = $model->table('region')->where(array('region_code'=>$address['did']))->find();
			$address['dis_name'] = $dis['region_name'];
		}else{
			$address['dis_name'] = '';
		}
		
		Tpl::output('order',$order);
		Tpl::output('auction',$auction);
		Tpl::output('address',$address);
		
		Tpl::showpage('payment.sendout');	
	}
	
	
	public function confirmtuikuanOp(){
		
		if(isset($_POST)&&!empty($_POST)){
		
			$model = Model();
			$order = $model->table('order')->where(array('id'=>intval($_POST['order_id'])))->find();
			
			if(empty($order)){
				echo json_encode(array('state'=>'0'));
				exit;
			}
			
			if($order['is_fail']==1){
				echo json_encode(array('state'=>'0'));
				exit;
			}
			
			$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
			$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();
			
			$price = $order['price'];
			if($auction['cashprice']>0){
				$seller = $model->table('user')->where(array('user_id'=>$order['seller_id']))->find();
					
				$data = array();
				$data1 = array();
				$data['touser'] = $seller['openid'];
				$data['template_id'] = $this->getTemplateId(6);
				$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&auction_id='.$auction['id'];
				$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的保证金额退回至余额";
				$data['data']['reason']['value'] = '退款';
				$data['data']['refund']['value'] = $auction['cashprice'].'元';
				$data['data']['remark']['value'] = '如未收到，请联系客服';
					
				$data1 = json_encode($data);
				$this->send_template_message($data1);
				$model->table('user')->where(array('user_id'=>$order['seller_id']))->setInc('price',$auction['cashprice']);
					
				$params = array();
				$params['user_id'] = $order['seller_id'];
				$params['mingxi_type'] = '0';
				$params['mingxi_price'] = $auction['cashprice'];
				$params['add_time'] = time();
				$params['mingxi_reason'] = '保证金退还';
				$params['mingxi_pos'] = '0';
				$model->table('mingxi')->insert($params);
					
				$price = $price+$auction['cashprice'];
			}
			
			$user['price']=$user['price']+$price;
			
			$model->table('user')->where(array('user_id'=>$user['user_id']))->update(array('price'=>$user['price']));
			$params = array();
			$params['is_fail'] = 1;
			$params['is_deal'] = 1;
			$params['finish_time'] = time();
			
			$model->table('order')->where(array('id'=>intval($_POST['order_id'])))->update($params);
			
			$data = array();
			$data1 = array();
			$data['touser'] = $user['openid'];
			$data['template_id'] = $this->getTemplateId(6);
			$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$order['id'];
			$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】,您的退款金额退回至余额";
			$data['data']['reason']['value'] = '退款';
			$data['data']['refund']['value'] = $price.'元';
			$data['data']['remark']['value'] = '如未收到退款，请联系客服';
			
			$data1 = json_encode($data);
			$this->send_template_message($data1);
			
			$params = array();
			$params['user_id'] = $user['user_id'];
			$params['mingxi_type'] = '0';
			$params['mingxi_price'] = $price;
			$params['add_time'] = time();
			$params['mingxi_reason'] = '拍品金额和保证金退还';
			$params['mingxi_pos'] = '0';
			$model->table('mingxi')->insert($params);
			
			echo json_encode(array('state'=>1));
			exit;
		
		}
		
		$model = Model();
		$order = $model->table('order')->where(array('id'=>intval($_GET['order_id'])))->find();		
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		$address = $model->table('address')->where(array('id'=>$order['order_address']))->find();		
		$prov = $model->table('region')->where(array('region_code'=>$address['pid']))->find();
		$address['prov_name'] = $prov['region_name'];
		
		$city = $model->table('region')->where(array('region_code'=>$address['cid']))->find();
		$address['city_name'] = $city['region_name'];
		
		if(!empty($address['did'])){
			$dis = $model->table('region')->where(array('region_code'=>$address['did']))->find();
			$address['dis_name'] = $dis['region_name'];
		}else{
			$address['dis_name'] = '';
		}
		
		if($order['seller_id']!=$_COOKIE['user_id']){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		Tpl::output('order',$order);
		Tpl::output('auction',$auction);
		Tpl::output('address',$address);
		
		Tpl::showpage('payment.confirmtuikuan');
	}
	
	
	public function commentOp(){
		
		if(isset($_POST)&&!empty($_POST)){
			$params = array();
			$params['order_comment'] = $_POST['comment'];
			$params['comment_time'] = time();
			$params['order_score'] = intval($_POST['stars']);
			
			$condition = array();
			$condition['id'] = $_POST['order_id'];
			$model = Model();
			$res = $model->table('order')->where($condition)->update($params);
			if($res){
				$order = $model->table('order')->where(array('id'=>$_POST['order_id']))->find();
				
				$data = array();
				$data['auction_id'] = $order['order_auction_id'];  
				$data['uid'] = $order['seller_id'];
				$data['message'] = '买家评价成功';
				$data['add_time'] = time();				
				$model->table('seller_log')->insert($data);

				$seller = $model->table('user')->where(array('user_id'=>$order['seller_id']))->find();
				$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
				
				$data = array();
				$data['touser'] = $seller['openid'];
				$data['template_id'] = $this->getTemplateId(7);
				$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$auction['id'];
				$data['data']['first']['value'] = "尊敬的【{$order['seller_name']}】";
				$data['data']['Content1']['value'] = "【{$order['user_name']}】对您的拍品进行了评价";
				$data['data']['Good']['value'] = "{$auction['auction_description']}";
				$data1 = json_encode($data);
				$d = $this->send_template_message($data1);

				echo json_encode(array('state'=>1));
			}
			exit;
		}
		
		$model = Model();
		$order = $model->table('order')->where(array('id'=>intval($_GET['order_id'])))->find();
		if(empty($order)){
			echo json_encode(array('state'=>'0'));
			exit;
		}

		if(($order['user_id']==$_COOKIE['user_id']) || ($order['seller_id']==$_COOKIE['user_id'])){
			Tpl::output('order',$order);
			
			$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
			Tpl::output('auction',$auction);
			
			$user = $model->table('user')->where(array('user_id'=>$auction['seller_id']))->find();
			Tpl::output('user',$user);
			
			Tpl::showpage('payment.comment');
		}else{
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
	}
	
	public function rechargeOp(){
		
		if(!empty($_POST)){
			$model = Model();
			$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();

			if(empty($user)){
				echo json_encode(array('state'=>0,'error'=>'用户不存在'));
				exit;
			}
			
			if((intval($_POST['price'])<=0) || (intval($_POST['price'])>20000)){
				echo json_encode(array('state'=>0,'error'=>'请填写正确的金额,金额范围在0-20000之间'));
				exit;
			}
			
			$params = array();
			$params['user_id'] = $_COOKIE['user_id'];
			$params['price'] = intval($_POST['price']);
			$params['add_time'] = time();
			$params['recharge_order_sn'] = 're'.$this->snOrder();
			$params['is_recharge'] = '0';
			
			$res = $model->table('recharge')->insert($params,true);
			
			if($res){
				echo json_encode(array('state'=>'1','recharge_id'=>$res));
			}else{
				echo json_encode(array('state'=>'0'));
			}			
			exit;
		}
		
		$model = Model();
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();

		if(empty($user['paypasswd'])){
			$this->kaitongOp();
			exit;
		}
		$ref_url = request_uri();
		Tpl::output('ref_url',$ref_url);
		Tpl::showpage('payment.recharge');
	}
	
	public function weixinrechargeOp(){
		
		$recharge_id = $_GET['recharge_id'];
		
		$model = Model();
		$recharge = $model->table('recharge')->where(array('id'=>$recharge_id))->find();
		Tpl::output('recharge',$recharge);

		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		Tpl::output('user',$user);
		
		Tpl::showpage('payment.weixin.recharge');
	}
	
	public function tuihuoOp(){
		
		if(isset($_POST)&&!empty($_POST)){
			$condition = array();
			$condition['id'] = intval($_POST['order_id']);
			$condition['user_id'] = $_COOKIE['user_id'];
			
			$params = array();
			$params['is_tuihuo'] = 1;
			$params['reason'] = $_POST['reason'];
			$params['tuihuo_time'] = time();
			
			$model = Model();
			$res = $model->table('order')->where($condition)->update($params);
			
			$order = $model->table('order')->where(array('id'=>intval($_POST['order_id'])))->find();
			$seller = $model->table('user')->where(array('user_id'=>$order['seller_id']))->find();
			$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
			
			$data = array();
			$data['touser'] = $seller['openid'];
			$data['template_id'] = $this->getTemplateId(15);
			
			$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$order['order_auction_id'];
			$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的拍品买家申请退货\n退货理由：".$_POST['reason'];
			$data['data']['orderProductPrice']['value'] = "¥{$order['price']}元";
			$data['data']['orderProductName']['value'] = "{$auction['auction_description']}";
			$data['data']['orderName']['value'] = "{$order['order_sn']}";
			$data['data']['remark']['value'] = '提醒：'.date("m月d日 H:i",($params['tuihuo_time']+48*3600)).'前未处理，买家进入发货流程';;
			$data1 = json_encode($data);
			$this->send_template_message($data1);
			
			if($res){
				$params1 = array();
				$params1['order_id'] = $order['id'];
				$params1['type'] = '0';
				$params1['user_id'] = $order['user_id'];
				$params1['user_name'] = $order['user_name'];
				$params1['refund_reason'] = $_POST['reason'];
				$params1['refund_add_time'] = time();
				
				$model->table('refund')->insert($params1);
				echo json_encode(array('state'=>1));
			}else{
				echo json_encode(array('state'=>0));
			}
			exit;
		}
		
		$condition = array();
		$condition['id'] = $_GET['order_id'];
		$condition['user_id'] = $_COOKIE['user_id'];
		
		$model = Model();
		$order = $model->table('order')->where($condition)->find();
		
		if(empty($order)){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		
		Tpl::output('order',$order);
		Tpl::output('auction',$auction);
		
		Tpl::showpage('payment.tuihuo');
	}
	
	/*
	 * 同意退货
	 */
	public function accepttuikuanOp(){
		
		if(isset($_POST)&&!empty($_POST)){
			
			$order_id = $_POST['order_id'];

			$condition = array();
			$condition['id'] = $order_id;

			$model = Model();
			$order = $model->table('order')->where($condition)->find();
			
			$params = array();
			$params['is_accept'] = 1;
			$params['accept_time'] = time();
			$params['tuihuo_address'] = $_POST['address_id'];
			
			$res = $model->table('order')->where($condition)->update($params);
			if($res){	
				$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
				$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();
				
				$data = array();
				$data1 = array();
				$data['touser'] = $user['openid'];
				$data['template_id'] = $this->getTemplateId(6);
				$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$order['id'];
				$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】,拍品“{$auction['auction_description']}”卖家已同意退货，请在".date("m月d日 H:i",($params['accept_time']+time())).'前，填写拍品退货单号。若超期，该拍品将自动确认收货！';
				$data['data']['reason']['value'] = '申请退货';
				$data['data']['refund']['value'] = "¥{$order['price']}元";
				$data['data']['remark']['value'] = '您的货款将在卖家收到拍品后退还（退货运费由您自行承担）';
					
				$data1 = json_encode($data);
				$this->send_template_message($data1);
				echo json_encode(array('state'=>1));
				exit;
			}
		}
		
		$auction_id = intval($_GET['auction_id']);
		$model = Model();
		$order = $model->table('order')->where(array('order_auction_id'=>$auction_id))->find();
		Tpl::output('order',$order);
		
		if($order['seller_id']!=$_COOKIE['user_id']){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		Tpl::output('auction',$auction);
		
		$condition_addr = array();
		$condition_addr['user_id'] = $_COOKIE['user_id'];
		$condition_addr['is_default'] = 1;
		$address = $model->table('address')->where($condition_addr)->find();
		
		$prov = $model->table('region')->where(array('region_code'=>$address['pid']))->find();
		$address['prov_name'] = $prov['region_name'];
		
		$city = $model->table('region')->where(array('region_code'=>$address['cid']))->find();
		$address['city_name'] = $city['region_name'];
		
		if(!empty($address['did'])){
			$dis = $model->table('region')->where(array('region_code'=>$address['did']))->find();
			$address['dis_name'] = $dis['region_name'];
		}else{
			$address['dis_name'] = '';
		}
		Tpl::output('address',$address);
		
		Tpl::showpage('payment.accepttuikuan');
	}
	
	
	
	public function refuseOp(){
		
		if(isset($_POST)&&!empty($_POST)){			
			$params = array();
			$params['is_accept'] = 2;
			$params['accept_time'] = time();
			$params['is_delay'] = 1;
			
			$condition = array();
			$condition['id'] = $_POST['order_id'];
			
			$model = Model();
			$res = $model->table('order')->where($condition)->update($params);
			if($res){
				
				$order = $model->table('order')->where(array('id'=>$_POST['order_id']))->find();
				$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();
				$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
				
				$params1 = array();
				$params1['order_id'] = $order['id'];
				$params1['type'] = '1';
				$params1['user_id'] = $order['seller_id'];
				$params1['user_name'] = $order['seller_name'];
				$params1['refund_reason'] = trim($_POST['reason']);
				$params1['refund_add_time'] = time();
				
				$model->table('refund')->insert($params1);
				
				$data = array();
				$data1 = array();
				$data['touser'] = $user['openid'];
				$data['template_id'] = $this->getTemplateId(16);
				$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$order['id'];
				$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】,您的退货申请，卖家未通过\n拒绝理由：".trim($_POST['reason']);;
				$data['data']['orderProductPrice']['value'] = "¥{$order['price']}元";
				$data['data']['orderProductName']['value'] = "{$auction['auction_description']}";
				$data['data']['orderName']['value'] = $order['order_sn'];
				$data['data']['remark']['value'] = '提醒：您如果与卖家无法达成一致，可申请小二介入，小二会联系您';
					
				$data1 = json_encode($data);
				$this->send_template_message($data1);
				
				echo json_encode(array('state'=>1));
				exit;
			}
			
		}
		
		$model = Model();
		$order = $model->table('order')->where(array('id'=>intval($_GET['order_id'])))->find();
		Tpl::output('order',$order);
		
		if($order['seller_id']!=$_COOKIE['user_id']){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		Tpl::output('auction',$auction);
		
		Tpl::showpage('payment.refuse');
	}
	
}